package io.emqx.cloud;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.Statement;

import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.JSON;

public class SaveMysql {
    private static String driver="com.mysql.jdbc.Driver";
    private static String url="jdbc:mysql://121.40.91.87:3306/emqtt?serverTimezone=UTC&useSSL=false";
    private static String user="root";
    private static String password="Hjx123456.";
    Connection conn=null;
    Statement stmt=null;
    java.sql.Timestamp timestamp=new java.sql.Timestamp(System.currentTimeMillis());
    JSONObject msg;

    public void savedate_esp(int Qos,String payload) {
         msg = JSON.parseObject(payload);
         String sql =
                "insert into esp(timestamp,temp, hum, qos)"+"values('"+timestamp+"','"+msg.getInteger("temp")+"' ,'"+msg.getInteger("hum")+"','"+Qos+"');";
        try {
            Class.forName(driver);
            conn=DriverManager.getConnection(url,user,password);
            stmt=conn.createStatement();
            stmt.executeUpdate(sql);
            //System.out.print("1111");
        }
        catch(Exception e) {
            e.printStackTrace();
        }
        finally {
            try {
                if(stmt!=null) stmt.close();
                if(conn!=null) conn.close();
            }
            catch(Exception e) {
                e.printStackTrace();
            }
        }
    }
    public void savedate_oled(int Qos,String payload) {

        msg =JSON.parseObject(payload);

        String sql =
                //  "insert into topic(Topic,Qos,Payload,Atime)"+"values('"+Client.TOPIC+"','"+Qos+"' ,'"+payload+"','"+timestamp+"');";
                "insert into oled(timestamp,msg, qos)"+"values('"+timestamp+"','"+msg.getString("msg")+"','"+Qos+"');";
        try {
            Class.forName(driver);
            conn=DriverManager.getConnection(url,user,password);
            stmt=conn.createStatement();
            stmt.executeUpdate(sql);
            //System.out.print("1111");
        }
        catch(Exception e) {
            e.printStackTrace();
        }
        finally {
            try {
                if(stmt!=null) stmt.close();
                if(conn!=null) conn.close();
            }
            catch(Exception e) {
                e.printStackTrace();
            }
        }
    }
    public void savedate_rgb(int Qos,String payload) {

        msg =JSON.parseObject(payload);

        String sql =
                //  "insert into topic(Topic,Qos,Payload,Atime)"+"values('"+Client.TOPIC+"','"+Qos+"' ,'"+payload+"','"+timestamp+"');";
                "insert into rgb(timestamp,R,G,B, qos)"+"values('"+timestamp+"','"+msg.getInteger("R")+"' ,'"+msg.getInteger("G")+"' ,'"+msg.getInteger("B")+"','"+Qos+"');";
        try {
            Class.forName(driver);
            conn=DriverManager.getConnection(url,user,password);
            stmt=conn.createStatement();
            stmt.executeUpdate(sql);
            //System.out.print("1111");
        }
        catch(Exception e) {
            e.printStackTrace();
        }
        finally {
            try {
                if(stmt!=null) stmt.close();
                if(conn!=null) conn.close();
            }
            catch(Exception e) {
                e.printStackTrace();
            }
        }
    }
    


}
